<?php

namespace backend\controllers;

use backend\models\EveryIsOk;
use backend\models\PermissionForm;
use backend\models\RoleForm;
use function foo\func;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\helpers\VarDumper;
use yii\web\Controller;
use yii\web\HttpException;

class RbacController extends Controller
{
    public function behaviors()
    {
        //通过acf配合RBAC实现权限控制
        return [
            'access'=>[
                'class'=>AccessControl::className(),
                //only表示值对指定的操作添加访问控制
                'only'=>['index','permission-list','add-permission','remove-permission','role-list','add-role','remove-role','edit-role'],
                'rules'=>[
                    [
                        'allow'=>true,
                        'actions'=>[
                            'index','permission-list',
                            'add-permission','remove-permission',
                            'role-list','add-role',
                            'remove-role','edit-role'
                        ],
                        //'roles'=>['admin'],
                        'matchCallback' => function ($rule,$action){
                            //判断当前用户是否拥有该操作的访问权限
                            if(\Yii::$app->user->can($this->route)){

                                //判断当前用户是否是游客,如果是,跳转到登录页面
                                if(\Yii::$app->user->isGuest){
                                    return $action->controller->redirect(\Yii::$app->user->loginUrl);
                                }
                                //没有权限,抛出一个403 forbidden 异常
                                throw new HttpException(403,'对不起,你没有该操作的权限');
                            }
                            return true;
                        }
                    ],
                    [
                        //
                    ],
                ]
            ]
        ];
    }

    public function actionIndex()
    {

        \Yii::$app->authManager;
        return $this->render('index');
    }


    /**
     * 展示权限列表
     * @return string
     */
    public function actionPermissionList()
    {
        //记住权限列表的url地址
        Url::remember(Url::current(),'plist');
        $auth = \Yii::$app->authManager;
        //获取所有的权限
        $permissions = $auth->getPermissions();


        return $this->render('permissionList',['permissions'=>$permissions]);
    }
    /**
     * 添加权限
     * @return string|\yii\web\Response
     */
    public function actionAddPermission()
    {

        /** @var  $model  '实例化表单模型' */
        $model = new PermissionForm();
        /** 接收数据 */
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
            //调用方法保存权限
            $rs = PermissionForm::savePermission($model->name,$model->description);
            //展示提示信息
            EveryIsOk::setTisiInfo($rs,'添加权限');
            //跳转
            return $this->redirect(['rbac/permission-list']);
        }
        return $this->render('addPermission',['model'=>$model]);

    }

    /**
     * 删除指定权限
     * @param $name
     * @return \yii\web\Response
     */
    public function actionRemovePermission($name){
        $auth = \Yii::$app->authManager;
        $permission = $auth->getPermission($name);
        //删除权限
        $r = empty($permission) ? false: $auth->remove($permission);
        //设置提示信息
        EveryIsOk::setTisiInfo($r,'删除权限');

        return $this->redirect(Url::previous('plist'));

     }


    /**
     * @return string
     */
    public function actionRoleList()
    {
        //记住当前的url
        Url::remember(Url::current(),'rlist');
        $auth =\Yii::$app->authManager;
        /** @var  $roles */
        $roles = $auth->getRoles();

        return $this->render('roleList',['roles'=>$roles]);
     }

    /**
     * @return string|\yii\web\Response
     */
    public function actionAddRole()
    {
        $auth = \Yii::$app->authManager;
        /** 实例化表单模型 */
        $model = new RoleForm();
        //设置场景
        $model->scenario = 'add';
        /** 接收数据 */
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
            //调用方法保存角色
            $model->saveRole();
            //跳转到指定页面
             return $this->redirect(['rbac/role-list']);
        }
        //渲染视图
        return $this->render('addRole',['model'=>$model]);
     }

    /**
     * @param $name
     * @return \yii\web\Response
     */
    public function actionRemoveRole($name)
    {
        $auth = \Yii::$app->authManager;

        $role = $auth->getRole($name);
        $rs = empty($role)?false: $auth->remove($role);
        EveryIsOk::setTisiInfo($rs,'删除权限');

        return $this->redirect(Url::previous('rlist'));
     }


    /**
     * @param $name
     * @return string|\yii\web\Response
     */
    public function actionEditRole($name){

         /** 实例化表单模型 */
         $model = new RoleForm();
         //调用方法加载数据
         $model->loadData($name);
         /** 接收数据 */
         if($model->load(\Yii::$app->request->post()) && $model->validate()){
             $model->name =$model->prevName;
             /** 把创建的角色保存到数据库 */
             $r = $model->saveRole();
             //设置提示信息
             EveryIsOk::setTisiInfo($r,$model->name.'角色修改');
             //跳转到指定页面
             return $this->redirect(['rbac/role-list']);
         }

         return $this->render('addRole',['model'=>$model]);
     }

}
